package com.example.administrator.demorecyclerview;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.demorecyclerview.auto_viewpager.HeaderFooterRecyclerView;
import com.example.administrator.demorecyclerview.auto_viewpager.MyPagerAdapter;
import com.example.administrator.demorecyclerview.auto_viewpager.MyViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private HeaderFooterRecyclerView mRecyclerView;
    private MyAdapter mAdapter;
    //    private final List<MyObject> mList = new ArrayList<>();
    private ListView mListView;

    private final Handler mHandler = new Handler();
    MyAdapter2 adapter2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        for (int i = 0; i < sparseArray.size(); ++i) {
//            Log.i("@@@@@", "@@@@@@@@@:" + sparseArray.valueAt(i));
//        }
//        for (int i = 0; i < 100; ++i) {
//            mList.add(new MyObject(i));
//        }

//        mListView = findViewById(R.id.listview);
//        mListView.setAdapter(new MyArrayAdapter(mList));

//        final SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
//        swipeRefreshLayout.setRefreshing(true);
//        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                Log.i("@@@@@@@","@@@@@@@@@@@:onRefresh");
////                swipeRefreshLayout.setRefreshing(false);
////                swipeRefreshLayout.setRefreshing(true);
//            }
//        });
        final LinearLayoutManager linearLayoutManager;
        mRecyclerView = findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(linearLayoutManager = new LinearLayoutManager(this));


        adapter2 = new MyAdapter2();
//        final TextView header = new TextView(this);
//        header.setText("header");
//        final TextView header2 = new TextView(this);
//        header2.setText("header2");
//        final ImageView footer = new ImageView(this);
//        final ImageView footer2 = new ImageView(this);
//        footer.setImageResource(R.mipmap.ic_launcher);
//        footer2.setImageResource(R.mipmap.ic_launcher);

//        mRecyclerView.setHeaderFooterAdapter(new View[]{header, header2}, new View[]{footer, footer2}, adapter2);


        mRecyclerView.setAdapter(adapter2);
//        mRecyclerView.addHeaderView(0,header);
//        mRecyclerView.addHeaderView(header2);
//        mRecyclerView.addFooterView(footer);
//        mRecyclerView.addFooterView(footer2);

//        mHandler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
////                for (int i = 100; i < 200; ++i) {
////                    mList.add(new MyObject(i));
////                }
////                adapter2.notifyItemRangeInserted(100, 100);
////                mRecyclerView.removeHeaderView(header);
////                mRecyclerView.removeHeaderView(header2);
//                mRecyclerView.removeFooterView(footer);
//                mRecyclerView.removeFooterView(footer2);
//
//            }
//        }, 5000);
//        Integer[][] arr = new Integer[100][10];
//
//        for (int i = 0; i < arr.length; ++i) {
//            for (int j = 0; j < arr[i].length; ++j) {
//                arr[i][j] = Integer.parseInt(String.valueOf(i) + "000000" + String.valueOf(j));
//            }
//        }
//
//        RecyclerViewPagerAdapter adapter = new RecyclerViewPagerAdapter();
//        adapter.data = arr;
//        mRecyclerView.setAdapter(adapter);

//        final FrameLayout frameLayout = findViewById(R.id.frame);
//
//
//        linearLayoutManager.scrollToPositionWithOffset(0, 0);
//        linearLayoutManager.setStackFromEnd(true);
//
//        mRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
//            @Override
//            public void onViewRecycled(RecyclerView.ViewHolder holder) {
//
//                if (holder.getAdapterPosition() == 0) {
//                    Log.i("@@@@@@", "@@@@@@@@@@@@@:onViewRecycled");
//                }
//
//
////                if (holder.getAdapterPosition() == 0) {
////                    frameLayout.addView(holder.itemView);
////                }
//
//
//            }
//        });
//
//        final TextView textView = new TextView(this);
//        textView.setText("0");
//        textView.setBackgroundColor(Color.WHITE);
//
//        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                Log.i("@@@@@@", "@@@@@@@@@@@@@:onScrolled:" + dx + "," + dy);
//
//                RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(1);
//                if (viewHolder != null) {
//                    final View v = viewHolder.itemView;
//                    final int upX = v.getTop() - recyclerView.getPaddingTop();
//                    if (upX < 0) {
//                        if (textView.getParent() == null) {
//                            frameLayout.addView(textView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//                            Log.i("@@@@@@", "@@@@@@@@@@@@@:add");
//
//                        }
//                    } else {
//                        if (textView.getParent() != null) {
//                            frameLayout.removeView(textView);
//                            Log.i("@@@@@@", "@@@@@@@@@@@@@:remove");
//
//                        }
//                    }
//
//
//                }
//
//
//            }
//        });

    }

    public void onClick1(View view) {

        adapter2.list.set(0, "00");
//        adapter2.notifyItemChanged(0);

        for (int i = 1; i < adapter2.list.size(); ) {
            adapter2.list.remove(i);
        }

        adapter2.notifyDataSetChanged();
    }

    public void onClick2(View view) {
    }

    private class MyAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        List<String> list = new ArrayList<>(50);

        public MyAdapter2() {
            for (int i = 0; i < 50; ++i) {
                list.add(String.valueOf(i));
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Log.i("@@@@@", "onCreateViewHolder" + viewType);

            final TextView textView = new TextView(parent.getContext());

            return new RecyclerView.ViewHolder(textView) {
                @Override
                public String toString() {
                    return super.toString();
                }
            };
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((TextView) holder.itemView).setText(list.get(position));
            Log.i("@@@@@", "onBindViewHolder" + position);

        }

        @Override
        public void onViewRecycled(RecyclerView.ViewHolder holder) {
            super.onViewRecycled(holder);

        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }


    static class RecyclerViewPagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        Integer[][] data;

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


            ViewPager viewPager = new MyViewPager(parent.getContext());
            viewPager.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500));

            return new RecyclerView.ViewHolder(viewPager) {
                @Override
                public String toString() {
                    return super.toString();
                }
            };
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewPager viewPager = (ViewPager) holder.itemView;
            if (viewPager.getAdapter() == null) {
                viewPager.setAdapter(new MyViewPagerAdapter(data[position]));
            } else {
                MyPagerAdapter adapter = (MyPagerAdapter) viewPager.getAdapter();
                adapter.mData = data[position];
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public int getItemCount() {
            return 10;
        }
    }


    static class MyViewPagerAdapter extends MyPagerAdapter<Integer> {
        public MyViewPagerAdapter(Integer[] data) {
            super(data);
        }

        @Override
        public View getView(ViewGroup container, View convertView, int position) {
            TextView textView;
            if ((textView = (TextView) convertView) == null) {
                textView = new TextView(container.getContext());
                textView.setBackgroundResource(R.drawable.ic_launcher_background);
                textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
            textView.setText(String.valueOf(mData[position]));
            return textView;
        }

        @Override
        public int getRealCount() {
            return 10;
        }

        @Override
        public long getScrollInterval() {
            return 0;
        }

        @Override
        public int getCurrentPointResource() {
            return 0;
        }

        @Override
        public int getOtherPointResource() {
            return 0;
        }
    }


    public interface OnValListener {
        void onValChanged(int val);
    }

    static class MyObject implements TextWatcher {

        public MyObject(int add) {
            this.add = add;
        }

        public void setListener(OnValListener listener) {
            this.listener = listener;
        }

        public OnValListener listener;


        @Override
        public String toString() {
            return String.valueOf(add);
        }

        final int add;

        int value;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            value = Integer.parseInt(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }


    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
        public MyAdapter(List<MyObject> mDatas) {
            this.mDatas = mDatas;
        }

        private final List<MyObject> mDatas;

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(new MyEditTextView(parent.getContext()));
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            MyEditTextView tv = (MyEditTextView) holder.itemView;
            final MyObject myObject = mDatas.get(position);
            myObject.setListener(new OnValListener() {
                @Override
                public void onValChanged(int val) {
                    Log.i("@@", "@@@:position:" + position + ",val:" + val);
                    for (int i = 0; i < position; ++i) {
                        mDatas.get(i).value = val + mDatas.get(i).add;
                    }
                    for (int i = position + 1; i < getItemCount(); ++i) {
                        mDatas.get(i).value = val + mDatas.get(i).add;
                    }

//                    if (!mRecyclerView.isComputingLayout()) {
//                        notifyDataSetChanged();
//                    }


//                    notifyDataSetChanged();

                }
            });
            tv.setTag(myObject);
            tv.setText(String.valueOf(mDatas.get(position).value));
        }


        @Override
        public int getItemCount() {
            return mDatas.size();
        }


        @Override
        public int getItemViewType(int position) {
            if (position % 2 == 0)
                return 0;
            return 1;
        }

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
            if (manager instanceof GridLayoutManager) {
                final GridLayoutManager gridManager = ((GridLayoutManager) manager);
                gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        final int c = position % 6;
                        if (c == 0) {
                            return 6;
                        }
                        if (c < 4) {
                            return 2;
                        }

                        return 3;
                    }
                });
            }
        }


        class MyViewHolder extends RecyclerView.ViewHolder {

            public MyViewHolder(View itemView) {
                super(itemView);
                itemView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT));
            }
        }
    }

    static class MyArrayAdapter extends BaseAdapter {

        private final List<String> mDatas;

        public MyArrayAdapter(List<String> mDatas) {
            this.mDatas = mDatas;
        }

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView;

            if ((textView = (TextView) convertView) == null) {
                textView = new TextView(parent.getContext());
            }
            textView.setText(String.valueOf(mDatas.get(position)));
            return textView;
        }
    }
}
